//
//  COFeedbackViewController.swift
//  CocoProject
//
//  Created by Ruthless on 2023/8/29.
//

import UIKit
import QMUIKit
import TZImagePickerController

class COFeedbackViewController: COBaseViewController {
    private var image: UIImage? {
        didSet {
            if let image = image {
                self.photoImageView.image = image
                self.deleteButton.isHidden = false
            } else {
                self.photoImageView.image = UIImage(named: "编组 13")
                self.deleteButton.isHidden = true
            }
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

    }
    
    override func configUI() {
        self.title = "Feedback"
        view.addSubview(photoNameLabel)
        view.addSubview(photoImageView)
        view.addSubview(deleteButton)
        view.addSubview(squareLabel)
        view.addSubview(textView)
        view.addSubview(submitButton)

        
        photoNameLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(22)
            make.top.equalToSuperview().offset(87+statusBarHeight)
        }
        
        photoImageView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(22)
            make.top.equalTo(photoNameLabel.snp.bottom).offset(10)
            make.width.height.equalTo(145)
        }
        
        deleteButton.snp.makeConstraints { make in
            make.right.top.equalTo(photoImageView).inset(10)
        }
        
        squareLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(22)
            make.top.equalTo(photoImageView.snp.bottom).offset(15)
        }
        
        textView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(22)
            make.top.equalTo(squareLabel.snp.bottom).offset(10)
            make.height.equalTo(195)
        }
        
        submitButton.snp.makeConstraints { make in
            make.bottom.equalToSuperview().offset(-32-bottomSafeHeight)
            make.left.right.equalToSuperview().inset(22)
            make.height.equalTo(50)
        }
    }

    private lazy var photoNameLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(14)
        label.textColor = .white
        label.text = "Photo"
        return label
    }()
    
    private lazy var photoImageView: UIImageView = {
        let imgV = UIImageView(image: UIImage(named: "编组 13"))
        imgV.layer.masksToBounds = true
        imgV.layer.cornerRadius = 10
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(selectImage))
        imgV.addGestureRecognizer(tap)
        imgV.isUserInteractionEnabled = true
        return imgV
    }()
    
    private lazy var deleteButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "编组 2"), for: .normal)
        btn.isHidden = true
        btn.addTarget(self, action: #selector(clickButton), for: .touchUpInside)
        return btn
    }()
    
    private lazy var squareLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(14)
        label.textColor = .white
        label.text = "Feedback content"
        return label
    }()
    
    private lazy var textView: UITextView = {
        let tv = UITextView()
        tv.backgroundColor = .rgba(41, 32, 63, 1)
        tv.layer.masksToBounds = true
        tv.layer.cornerRadius = 15
        tv.textColor = .white
        return tv
    }()
    
    private lazy var submitButton: UIButton = {
        let btn = UIButton()
        let bgLayer1 = CAGradientLayer()
        bgLayer1.colors = [UIColor.rgba(218, 67, 255).cgColor, UIColor.rgba(82, 28, 255).cgColor]
        bgLayer1.locations = [0, 1]
        bgLayer1.frame = CGRect(x: 0, y: 0, width: SCREENWIDTH-38, height: 50)
        bgLayer1.startPoint = CGPoint(x: 1, y: 0.56)
        bgLayer1.endPoint = CGPoint(x: 0.56, y: 0.56)
        btn.layer.insertSublayer(bgLayer1, at: 0)
        
        btn.setTitle("Submit", for: .normal)
        btn.titleLabel?.font = .medium(14)
        btn.setTitleColor(.white, for: .normal)
        
        btn.layer.masksToBounds = true
        btn.layer.cornerRadius = 25
        
        btn.addTarget(self, action: #selector(clickButton), for: .touchUpInside)
        return btn
    }()
}

extension COFeedbackViewController {
    @objc
    private func clickButton(_ btn: UIButton) {
        if btn == deleteButton {
            self.image = nil
        } else if btn == submitButton {
//            self.uploadImage()
            self.view.makeToastActivity(.center)
            DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: {
                self.view.hideToastActivity()
                self.view.makeToast("Success", position: .center) { _ in
                    self.navigationController?.popViewController(animated: true)
                }
            })
        }
    }
    @objc
    private func selectImage() {
        let picker = TZImagePickerController(maxImagesCount: 1, delegate: self)!
        picker.modalPresentationStyle = .fullScreen
        self.present(picker, animated: true)
    }
}

extension COFeedbackViewController: TZImagePickerControllerDelegate {
    func imagePickerController(_ picker: TZImagePickerController!, didFinishPickingPhotos photos: [UIImage]!, sourceAssets assets: [Any]!, isSelectOriginalPhoto: Bool, infos: [[AnyHashable : Any]]!) {
        self.image = photos.first
    }
}

extension COFeedbackViewController {
    private func uploadImage() {
        guard let image = image else {
            self.view.makeToast("Please select photo", position: .center)
            return
        }
        
        self.view.makeToastActivity(.center)
        CONetwork.sendRequest(provider: homeProvider, target: .uploadImage(image: image)) { text in
            let photo = text.replacingOccurrences(of: "\"", with: "")
            self.submit(photo)
        } failedBlock: { _, text in
            self.view.hideToastActivity()
            self.view.makeToast(text, position: .center)
        }
    }
    
    private func submit(_ text: String) {
//        CONetwork.sendRequest(provider: homeProvider, target: .create(photo: text, content: textView.text ?? "", type: select.rawValue, category: type+1)) { _ in
//            self.view.hideToastActivity()
//            self.navigationController?.popViewController(animated: true)
//        } failedBlock: { _, text in
//            self.view.hideToastActivity()
//            self.view.makeToast(text, position: .center)
//        }

    }
}
